package com.jeestudio.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AESUtil {

    private static String AES_KEY_ONE = "87C4@6%4!23JUhds";

    /**
     * CBC加密
     * @param data
     * @return
     */
    public static String CBCEncrypt(String data){
        String s = AESProviderUtil.CBCEncrypt(data, AES_KEY_ONE);
        return s;
    }

    /**
     * CBC解密
     * @param data
     * @return
     */
    public static String CBCDecrypt(String data){
        String s = AESProviderUtil.CBCDecrypt(data, AES_KEY_ONE);
        return s;
    }

    /**
     * 判断是否银行卡号或者身份证号，如果加密则解密，否则返回原值
     * @param data
     * @return
     */
    public static String CBCDecryptOrigin(String data){
        Pattern pattern = Pattern.compile("^\\d+(\\d|X|x)$");
        Matcher isNum = pattern.matcher(data);
        if(!isNum.matches()){
            String result = AESUtil.CBCDecrypt(data);
            return StringUtils.isNotBlank(result)?result:data;
        }
        return data;
    }

    public static void main(String[] arge){
        String s = AESUtil.CBCEncrypt("13718894581");
        System.out.println("#"+s+"#");
        System.out.println(AESUtil.CBCDecrypt(s));
        String s2 = AESUtil.CBCEncrypt("1234");
        System.out.println("#"+s2+"#");
        System.out.println(AESUtil.CBCDecrypt("fQbCrWjk2CGPAJoDOCkubmXMNGcygWDGiuphXEjfioM="));
    }
}
